home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / rpclib / clnt_generic.c < prev    next >
C/C++ Source or Header  |  1994-03-09  |  3KB  |  120 lines

  1. /*
  2.  * $Id: clnt_generic.c,v 1.2 1993/11/10 01:27:38 jraja Exp $
  3.  *
  4.  * $Log: clnt_generic.c,v $
  5.  * Revision 1.2  1993/11/10  01:27:38  jraja
  6.  * Added include sys/param.h. Fixed some types.
  7.  *
  8.  */
  9. /* @(#)clnt_generic.c    2.2 88/08/01 4.0 RPCSRC */
  10. /*
  11.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  12.  * unrestricted use provided that this legend is included on all tape
  13.  * media and as a part of the software program in whole or part.  Users
  14.  * may copy or modify Sun RPC without charge, but are not authorized
  15.  * to license or distribute it to anyone else except as part of a product or
  16.  * program developed by the user.
  17.  * 
  18.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  19.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  20.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  21.  * 
  22.  * Sun RPC is provided with no support and without any obligation on the
  23.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  24.  * modification or enhancement.
  25.  *
  26.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  27.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  28.  * OR ANY PART THEREOF.
  29.  * 
  30.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  31.  * or profits or other special, indirect and consequential damages, even if
  32.  * Sun has been advised of the possibility of such damages.
  33.  * 
  34.  * Sun Microsystems, Inc.
  35.  * 2550 Garcia Avenue
  36.  * Mountain View, California  94043
  37.  */
  38. #if !defined(lint) && defined(SCCSIDS)
  39. static char sccsid[] = "@(#)clnt_generic.c 1.4 87/08/11 (C) 1987 SMI";
  40. #endif
  41. /*
  42.  * Copyright (C) 1987, Sun Microsystems, Inc.
  43.  */
  44. #include <sys/param.h>
  45. #include <rpc/rpc.h>
  46. #include <sys/socket.h>
  47. #include <sys/errno.h>
  48. #include <netdb.h>
  49.  
  50. /*
  51.  * Generic client creation: takes (hostname, program-number, protocol) and
  52.  * returns client handle. Default options are set, which the user can 
  53.  * change using the rpc equivalent of ioctl()'s.
  54.  */
  55. CLIENT *
  56. clnt_create(hostname, prog, vers, proto)
  57.     char *hostname;
  58.     u_long prog;
  59.     u_long vers;
  60.     char *proto;
  61. {
  62.     struct hostent *h;
  63.     struct protoent *p;
  64.     struct sockaddr_in sin;
  65.     int sock;
  66.     struct timeval tv;
  67.     CLIENT *client;
  68.  
  69.     h = gethostbyname(hostname);
  70.     if (h == NULL) {
  71.         rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
  72.         return (NULL);
  73.     }
  74.     if (h->h_addrtype != AF_INET) {
  75.         /*
  76.          * Only support INET for now
  77.          */
  78.         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  79.         rpc_createerr.cf_error.re_errno = EAFNOSUPPORT; 
  80.         return (NULL);
  81.     }
  82.     sin.sin_family = h->h_addrtype;
  83.     sin.sin_port = 0;
  84.     bzero(sin.sin_zero, sizeof(sin.sin_zero));
  85.     bcopy(h->h_addr, (char*)&sin.sin_addr, h->h_length);
  86.     p = getprotobyname(proto);
  87.     if (p == NULL) {
  88.         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
  89.         rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; 
  90.         return (NULL);
  91.     }
  92.     sock = RPC_ANYSOCK;
  93.     switch (p->p_proto) {
  94.     case IPPROTO_UDP:
  95.         tv.tv_sec = 5;
  96.         tv.tv_usec = 0;
  97.         client = clntudp_create(&sin, prog, vers, tv, &sock);
  98.         if (client == NULL) {
  99.             return (NULL);
  100.         }
  101.         tv.tv_sec = 25;
  102.         clnt_control(client, CLSET_TIMEOUT, (caddr_t)&tv);
  103.         break;
  104.     case IPPROTO_TCP:
  105.         client = clnttcp_create(&sin, prog, vers, &sock, 0, 0);
  106.         if (client == NULL) {
  107.             return (NULL);
  108.         }
  109.         tv.tv_sec = 25;
  110.         tv.tv_usec = 0;
  111.         clnt_control(client, CLSET_TIMEOUT, (caddr_t)&tv);
  112.         break;
  113.     default:
  114.         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  115.         rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; 
  116.         return (NULL);
  117.     }
  118.     return (client);
  119. }
  120.